package bb.lanxing.util;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewItemClickHelper implements RecyclerView.OnChildAttachStateChangeListener, View.OnClickListener, View.OnLongClickListener {
    private OnItemClickListener clickListener;
    private OnItemLongClickListener longClickListener;
    private RecyclerView recyclerView;

    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder viewHolder);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(RecyclerView.ViewHolder viewHolder);
    }

    public RecyclerViewItemClickHelper(OnItemClickListener onItemClickListener, OnItemLongClickListener onItemLongClickListener) {
        this.clickListener = onItemClickListener;
        this.longClickListener = onItemLongClickListener;
    }

    public void attachToRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        recyclerView.addOnChildAttachStateChangeListener(this);
        if (recyclerView.isAttachedToWindow()) {
            int childCount = recyclerView.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = recyclerView.getChildAt(i);
                if (this.clickListener != null) {
                    childAt.setOnClickListener(this);
                }
                if (this.longClickListener != null) {
                    childAt.setOnLongClickListener(this);
                }
            }
        }
    }

    public void destroy() {
        int childCount = this.recyclerView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = this.recyclerView.getChildAt(i);
            childAt.setOnClickListener(null);
            childAt.setOnLongClickListener(null);
        }
        this.recyclerView.removeOnChildAttachStateChangeListener(this);
        this.recyclerView = null;
        this.clickListener = null;
        this.longClickListener = null;
    }

    @Override
    public void onChildViewAttachedToWindow(View view) {
        if (this.clickListener != null) {
            view.setOnClickListener(this);
        }
        if (this.longClickListener != null) {
            view.setOnLongClickListener(this);
        }
    }

    @Override
    public void onChildViewDetachedFromWindow(View view) {
        view.setOnClickListener(null);
        view.setOnLongClickListener(null);
    }

    @Override
    public void onClick(View view) {
        RecyclerView recyclerView = this.recyclerView;
        if (recyclerView == null) {
            return;
        }
        recyclerView.post(new PerformClick(view));
    }

    @Override
    public boolean onLongClick(View view) {
        RecyclerView recyclerView = this.recyclerView;
        if (recyclerView == null) {
            return false;
        }
        return recyclerView.post(new PerformLongClick(view));
    }

    private class PerformClick implements Runnable {
        private View clickedChildView;

        public PerformClick(View view) {
            this.clickedChildView = view;
        }

        @Override
        public void run() {
            RecyclerView.ViewHolder childViewHolder;
            View view = this.clickedChildView;
            this.clickedChildView = null;
            if (RecyclerViewItemClickHelper.this.recyclerView == null || view == null || (childViewHolder = RecyclerViewItemClickHelper.this.recyclerView.getChildViewHolder(view)) == null || RecyclerViewItemClickHelper.this.clickListener == null) {
                return;
            }
            RecyclerViewItemClickHelper.this.clickListener.onItemClick(childViewHolder);
        }
    }

    private class PerformLongClick implements Runnable {
        private View clickedChildView;

        public PerformLongClick(View view) {
            this.clickedChildView = view;
        }

        @Override
        public void run() {
            RecyclerView.ViewHolder childViewHolder;
            View view = this.clickedChildView;
            this.clickedChildView = null;
            if (RecyclerViewItemClickHelper.this.recyclerView == null || view == null || (childViewHolder = RecyclerViewItemClickHelper.this.recyclerView.getChildViewHolder(view)) == null || RecyclerViewItemClickHelper.this.longClickListener == null) {
                return;
            }
            RecyclerViewItemClickHelper.this.longClickListener.onItemLongClick(childViewHolder);
        }
    }
}